<div class="container">
    <h1 id="pkg-overview">package errors</h1>
    <p><code>import "errors"</code>
    </p><p>errors包实现了创建错误值的函数。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-package">
            <div class="panel-heading" onclick="document.getElementById('ex-package').style.display = document.getElementById('ex-package').style.display=='none'?'block':'none';">Example</div>
            <div class="panel-collapse collapse" id="ex-package">
                <div class="panel-body">
                    <pre>package errors_test
import (
    "fmt"
    "time"
)
<span class="com">// MyError is an error implementation that includes a time and message.</span>
type MyError struct {
    When time.Time
    What string
}
func (e MyError) Error() string {
    return fmt.Sprintf("%v: %v", e.When, e.What)
}
func oops() error {
    return MyError{
        time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
        "the file system has gone away",
    }
}
func Example() {
    if err := oops(); err != nil {
        fmt.Println(err)
    }
    <span class="com">// Output: 1989-03-15 22:30:00 +0000 UTC: the file system has gone away</span>
}
</pre>
                </div>
            </div>
        </div>
    </div>
    
    
		
        
    
    
    
		
        
        
        
    
    <h2 id="New">func <a href="https://github.com/golang/go/blob/master/src/errors/errors.go?name=release#9" title="View Source">New</a> </h2>
    <pre class="funcdecl">func New(text <a href="builtin.htm#string">string</a>) <a href="builtin.htm#error">error</a></pre>
    <p>使用字符串创建一个错误,请类比fmt包的Errorf方法，差不多可以认为是New(fmt.Sprintf(...))。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-New">
            <div class="panel-heading" onclick="document.getElementById('ex-New').style.display = document.getElementById('ex-New').style.display=='none'?'block':'none';">Example</div>
            <div class="panel-collapse collapse" id="ex-New">
                <div class="panel-body">
                    <pre>err := errors.New("emit macho dwarf: elf header corrupted")
if err != nil {
    fmt.Print(err)
}</pre>
                    <p>Output:
                    </p><pre>emit macho dwarf: elf header corrupted
</pre>
                </div>
            </div>
        </div>
        <div class="panel panel-default" id="example-New--Errorf">
            <div class="panel-heading" onclick="document.getElementById('ex-New--Errorf').style.display = document.getElementById('ex-New--Errorf').style.display=='none'?'block':'none';">Example (Errorf)</div>
            <div class="panel-collapse collapse" id="ex-New--Errorf">
                <div class="panel-body">
                    <pre>const name, id = "bimmler", 17
err := fmt.Errorf("user %q (id %d) not found", name, id)
if err != nil {
    fmt.Print(err)
}</pre>
                    <p>Output:
                    </p><pre>user "bimmler" (id 17) not found
</pre>
                </div>
            </div>
        </div>
    </div>
</div>